package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.*;

/**
 * Servlet implementation class InsertStudentServlet
 */
@WebServlet("/InsertStudentServlet")
public class InsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public InsertServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String ID = request.getParameter("ID");
		String Name = request.getParameter("Name");
		String Depart = request.getParameter("Depart");
		String Sex = request.getParameter("Sex");
		String Type = request.getParameter("Type");
		boolean a = AdminSql.insertCustomer(ID, Name, Depart, Sex,Type);
		if(Type.equals("student")){
			if(a){
				response.setHeader("refresh", "1;URL=Manage_Students.jsp?pages=1");
			} else {
				out.println("插入失败");
				response.setHeader("refresh", "1;URL=Insert_Customer.jsp?pages=1");
			}
		}else {
			if(a){
				response.setHeader("refresh", "1;URL=Manage_Teachers.jsp?pages=1");
			}else {
				out.println("插入失败");
				response.setHeader("refresh", "1;URL=Insert_Customer.jsp?pages=1");
			}

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
